<?php
namespace App\Controller;

class ToolsController extends AppController{

    public $_page_num = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：新增工具
   * */
    public function add()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        $tool = $this->Tools->newEntity();
        $tool->title = $data['title'];
        $tool->intro = $data['intro'];
        $tool->website = $data['website'];
        $tool->icon = $this->G->setImgUrl($data['icon']);
        $tool->deal_code = $data['deal_code'];
        $tool->download_url = $data['download_url'];
        $tool->order_num = $data['order_num'];
        $tool->enabled = $data['enabled'];
        $tool->add_time = time();
        $tool_save = $this->Tools->save($tool);
        $this->loadComponent('TextareaCache');
        $this->loadComponent('ContentFormat');
        $content  = $this->ContentFormat->clean($data['content'])[0];
        $this->TextareaCache->add('tools', $tool_save->id, $content);
        $this->loadComponent('ToolCache');
        $this->ToolCache->deleteList();
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：修改工具
   * */
    public function update()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        $tools = $this->Tools->findById($data['id'])->select(['id'])->first();
        if(empty($tools)){
            $this->G->error("tool_error");
            return null;
        }
        $tool = $this->Tools->newEntity();
        $tool->id = $data['id'];
        $tool->title = $data['title'];
        $tool->intro = $data['intro'];
        $tool->website = $data['website'];
        $tool->icon = $this->G->setImgUrl($data['icon']);
        $tool->deal_code = $data['deal_code'];
        $tool->download_url = $data['download_url'];
        $tool->order_num = $data['order_num'];
        $tool->enabled = $data['enabled'];
        $this->Tools->save($tool);
        $this->loadComponent('TextareaCache');
        $this->loadComponent('ContentFormat');
        $content  = $this->ContentFormat->clean($data['content'])[0];
        $text_area = $this->TextareaCache->getVal('tools', $data['id']);
        if(!empty($text_area)){
            $this->TextareaCache->update('tools', $data['id'], $content);
        }else{
            $this->TextareaCache->add('tools', $data['id'], $content);
        }
        $this->loadComponent('ToolCache');
        $this->ToolCache->deleteCache($data['id']);
        $this->ToolCache->deleteList();
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：获取工具信息
   * */
    public function getInfo()
    {
        $id = $this->request->getData('id');
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $tools = $this->Tools->findById($id)->first();
        if(empty($tools)){
            $this->G->error("tool_error");
            return null;
        }
        $tools['icon'] = empty($tools['icon']) ? '' : $this->G->getImgUrl($tools['icon']);
        $this->loadComponent('TextareaCache');
        $content = $this->TextareaCache->getVal('tools', $id);
        $this->loadComponent('ContentFormat');
        $tools['content']  = empty($content) ? '' : $this->ContentFormat->add($content['content']);
        $this->G->success("Success", $tools);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：删除工具
   * */
    public function delete()
    {
        if(!$this->request->is("post")){
            $this->G->error("post_error");
            return null;
        }
        $ids = $this->request->getData("ids");
        if(empty($ids) || !is_array($ids)){
            $this->G->error("ids_error");
            return null;
        }
        $this->loadComponent('ToolCache');
        foreach ($ids as $k=>$v){
            $this->ToolCache->deleteCache($v);
        }
        $this->Tools->deleteAll(['id in'=>$ids]);
        $this->ToolCache->deleteList();
        $this->G->success("success");
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.04.30
   * ps：获取工具列表
   * */
    public function getList()
    {
        $data = $this->request->getData();
        $this->loadComponent('ToolCache');
        if(!empty($data)){
            ksort($data, SORT_NATURAL);
        }
        $key = md5(json_encode($data));
        $data = $this->ToolCache->getList($data);
        $list = ['pageTotal'=>0,'list'=>[]];
        if(empty($data[$key]['list'])){
            $this->G->success('Success', $list);
            return null;
        }
        $list['pageTotal'] = floor($data[$key]['total']/$this->_page_num);
        foreach ($data[$key]['list'] as $k=>$v){
            $tool = $this->ToolCache->getVal($v);
            if(empty($tool)){
                continue;
            }
            $list['list'][] = $tool;
        }
        $this->G->success('Success', $list);
    }

}
?>